export const paging = {  //分页混入
    data() {
        return {
            pageNum: 1,
            pageSize: 10,
            current:1,
            pagination: {
                //表格分页相关配置
                current:1,
                total: 0,
                defaultPageSize: 10,
                showTotal: total => `共 ${total} 条数据`,
                showSizeChanger: true,
                pageSizeOptions: ["10", "20"],
                onShowSizeChange: (current, pageSize) => (this.pageSize = pageSize),
                onChange : (page) =>(this.pagination.current=page)
            }
        };
    },
    methods: {
        getLists(fn, param) {
            fn(param).then(res => {
                this.list = res.data.data.records
            }).catch(err => {
                this.$message.error(err);
            })
        },
        deleteAfterPageNum(delNum){//判断是否删除的是一页的最后一条数据
            //判断是单个删除还是多个删除
            const number = delNum === null?1:delNum
            const deleteAfterCurrent = Math.ceil((this.pagination.total - number) / this.pageSize) // 总页数
            this.pagination.current = this.pagination.current > deleteAfterCurrent ? deleteAfterCurrent : this.pagination.current
            this.pagination.current = this.pagination.current < 1 ? 1 : this.pagination.current
            this.pageNum = this.pagination.current > deleteAfterCurrent ? deleteAfterCurrent : this.pagination.current
            this.pageNum = this.pagination.current < 1 ? 1 : this.pagination.current
        },
    }
};


export const suncnWebSockte = {  //websockte混入
    data() {
        return {
            websock: null,
        };
    },
    created() {
        this.initWebSocket();//进入页面初始化websockte
    },
    methods: {
        initWebSocket() { //初始化weosocket
            const wsuri = "ws://192.168.2.31:8762/ws/websocket?" +localStorage.getItem("TOKEN");//链接weosocket地址
            this.websock = new WebSocket(wsuri);
            this.websock.onmessage = this.websocketonmessage;
            this.websock.onopen = this.websocketonopen;
            this.websock.onerror = this.websocketonerror;
            this.websock.onclose = this.websocketclose;
        },
        websocketonopen() { //连接建立之后执行send方法发送数据
            console.log("websocket链接成功")
            // let actions = { "test": "12345" };
            // this.websocketsend(JSON.stringify(actions));
        },
        websocketonerror() {//连接建立失败重连
            this.initWebSocket();
        },
        websocketonmessage(e) {//数据接收
            let data = JSON.parse(e.data);
            this.$notification.open({
                message: '消息通知',
                description: h => {
                    return h(
                        'span',
                        {
                        on: {
                            click: () => {
                                this.viewMsg({id:data.id,state:0});
                            }
                        },
                        },
                        data.content,
                    );
                },
                placement: 'bottomRight',
                bottom: '50px',
                duration: 5,
                class:'msg-notice'
            });
            this.getMsg(0);
        },
        websocketsend(Data) {//数据发送
            this.websock.send(Data);
        },
        websocketclose() {  //关闭
            console.log('断开连接');
        }
    },
    destroyed() {
        this.websock.close() //离开路由之后断开websocket连接
    },
};